package com.hboxs.asl.entity;

import org.hibernate.validator.constraints.NotBlank;

import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import javax.validation.constraints.NotNull;
import java.util.Set;

/**
 * Entity - 反馈意见
 */
@Entity
@Table(name = "xx_feedback")
@SequenceGenerator(name = "sequenceGenerator", sequenceName = "xx_feedback_sequence")
public class Feedback extends BaseEntity {

    private static final long serialVersionUID = 2402537061730592379L;

    public Feedback() {
    }

    public Feedback(String content, Member member) {
        this.content = content;
        this.member = member;
    }

    /**
     * 反馈的内容
     */
    private String content;

    /**
     * 反馈创建人
     */
    private Member member;

    @NotBlank(message = "反馈内容不能为空!")
    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    @NotNull(message = "发送人信息不能为空!")
    @ManyToOne
    public Member getMember() {
        return member;
    }

    public void setMember(Member member) {
        this.member = member;
    }

    public static String validate(Feedback feedback) {
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        Validator validator = factory.getValidator();
        Set<ConstraintViolation<Feedback>> constraintViolations = validator.validate(feedback);
        if (constraintViolations.size() > 0) {
            return constraintViolations.iterator().next().getMessage();
        }
        return null;
    }
}
